<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="操作地址" prop="operIp">
        <el-input
          v-model="queryParams.operIp"
          placeholder="请输入操作地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="子系统" prop="system">
        <el-select
          placeholder="请输入子系统"
          v-model="queryParams.sysCode"
          clearable
          @change="changeSystemCode"
        >
          <el-option
            v-for="dict in gen_sys_list"
            :key="dict.label"
            :label="dict.label"
            :value="dict.value"
            @click="changeSystemName(dict.label)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="功能模块" prop="model">
        <el-select
          placeholder="请选择功能模块"
          v-model="queryParams.modelCode"
          clearable
          @change="changeModelCode"
        >
          <el-option
            v-for="dict in modelList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            @click="changeModelName(dict.dictLabel)"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="business">
        <template #label> 业务模块</template>
        <el-select
          placeholder="请选择业务模块"
          v-model="queryParams.businessCode"
          clearable
          @change="changeBusinessCode"
        >
          <el-option
            v-for="dict in businessList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            @click="changeBusinessName(dict.dictLabel)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="功能名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入功能名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人员" prop="operName">
        <el-input
          v-model="queryParams.operName"
          placeholder="请输入操作人员"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作类型" prop="businessType">
        <el-input
          v-model="queryParams.businessType"
          placeholder="请输入操作类型"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择操作状态" clearable>
          <el-option
            v-for="dict in sys_common_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作时间">
        <el-date-picker
          v-model="operTimeDateRange"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 230px"
          :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['web:monitor:audit:operlog:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @query-table="getList" />
    </el-row>

    <el-table
      ref="operlogRef"
      v-loading="loading"
      :data="operlogList"
      height="640"
      @selection-change="handleSelectionChange"
      :default-sort="defaultSort"
      @sort-change="handleSortChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="日志编号" align="center" prop="operId" />
      <el-table-column label="子系统" align="center" prop="system" :show-overflow-tooltip="true" />
      <el-table-column label="功能模块" align="center" prop="model" :show-overflow-tooltip="true" />
      <el-table-column
        label="业务模块"
        align="center"
        prop="business"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="功能名称" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="操作类型" align="center" prop="businessType" />
      <el-table-column
        label="操作人员"
        align="center"
        width="110"
        prop="operName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作地址"
        align="center"
        prop="operIp"
        width="130"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_common_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" prop="operTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.operTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="消耗时间"
        align="center"
        prop="costTime"
        width="110"
        :show-overflow-tooltip="true"
      >
        <template #default="scope">
          <span>{{ scope.row.costTime }}毫秒</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleView(scope.row, scope.index)"
            v-hasPermi="['web:monitor:audit:operlog:info']"
            >详细
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="pageParams.pageNum"
      v-model:limit="pageParams.pageSize"
      @pagination="getList"
    />

    <!-- 操作日志详细 -->
    <el-dialog title="操作日志详细" v-model="open" width="1000px" append-to-body>
      <el-form :model="form" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="操作模块："
              >{{ form.system }} / {{ form.model }} / {{ form.business }} / {{ form.title }} /
              {{ typeFormat(form) }}
            </el-form-item>
            <el-form-item label="登录信息："
              >{{ form.operName }} / {{ form.operIp }} / {{ form.operLocation }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="请求地址：">
              <h style="overflow: auto; word-break: break-all">{{ form.operUrl }}</h>
            </el-form-item>
            <el-form-item label="请求方式：">{{ form.requestMethod }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="操作方法：">{{ form.method }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="请求参数：">{{ form.operParam }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="返回参数：">
              <h style="overflow: auto; word-break: break-all">{{ form.jsonResult }}</h>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作状态：">
              <div v-if="form.status === 0">正常</div>
              <div v-else-if="form.status === 1">失败</div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="消耗时间：">{{ form.costTime }}毫秒</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作时间：">{{ parseTime(form.operTime) }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="异常信息：" v-if="form.status === 1"
              >{{ form.errorMsg }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Operlog">
  import { ref, reactive, toRefs, getCurrentInstance } from 'vue';
  import { getDicts } from '@/api/system/dict/data';
  import { list, exportOperlog } from '@/api/monitor/operlog';

  const { proxy } = getCurrentInstance();
  const { sys_oper_type, sys_common_status } = proxy.useDict('sys_oper_type', 'sys_common_status');

  const operlogList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  // const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  // const title = ref('');
  const operTimeDateRange = ref([]);
  const defaultSort = ref({ prop: 'operTime', order: 'descending' });
  const { gen_sys_list } = proxy.useDict('gen_sys_list');
  const modelList = ref([]);
  const businessList = ref([]);

  const data = reactive({
    form: {},
    queryParams: {
      system: undefined,
      model: undefined,
      operIp: undefined,
      title: undefined,
      operName: undefined,
      businessType: undefined,
      status: undefined,
      sysCode: undefined,
      modelCode: undefined,
      businessCode: undefined,
    },
    pageParams: {
      pageNum: 1,
      pageSize: 10,
    },
  });

  const { queryParams, form, pageParams } = toRefs(data);

  /** 查询登录日志 */
  function getList() {
    loading.value = true;
    list(
      proxy.addFieldTimeRange(queryParams.value, operTimeDateRange.value, 'operTime'),
      pageParams.value,
    ).then((response) => {
      loading.value = false;
      if (response.data) {
        operlogList.value = response.data.records ? response.data.records : [];
        total.value = response.data.total ? response.data.total : 0;
      } else {
        operlogList.value = [];
        total.value = 0;
      }
    });
  }

  /** 操作日志类型字典翻译 */
  function typeFormat(row) {
    return proxy.selectDictLabel(sys_oper_type.value, row.businessType);
  }

  /** 搜索按钮操作 */
  function handleQuery() {
    pageParams.value.pageNum = 1;
    getList();
  }

  /** 重置按钮操作 */
  function resetQuery() {
    operTimeDateRange.value = [];
    proxy.resetForm('queryRef');
    queryParams.value.sysCode = undefined;
    queryParams.value.modelCode = undefined;
    queryParams.value.businessCode = undefined;
    pageParams.value.pageNum = 1;
    proxy.$refs['operlogRef'].sort(defaultSort.value.prop, defaultSort.value.order);
  }

  /** 多选框选中数据 */
  function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.operId);
    multiple.value = !selection.length;
  }

  /** 排序触发事件 */
  function handleSortChange(column) {
    queryParams.value.orderByColumn = column.prop;
    queryParams.value.isAsc = column.order;
    getList();
  }

  /** 详细按钮操作 */
  function handleView(row) {
    open.value = true;
    form.value = row;
  }

  /** 导出按钮操作 */
  function handleExport() {
    exportOperlog(
      proxy.addFieldTimeRange(queryParams.value, operTimeDateRange.value, 'operTime'),
      pageParams.value,
    );
  }

  // 点击联动下拉第一个下拉框触发方法
  function changeSystemCode() {
    queryParams.value.model = undefined;
    queryParams.value.business = undefined;
    initModelList();
  }

  function changeSystemName(system) {
    queryParams.value.system = system;
  }

  function initModelList() {
    getDicts(`gen_sys_item_${queryParams.value.sysCode}`).then((res) => {
      modelList.value = res.data;
    });
  }

  // 点击联动下拉第二个下拉框触发方法
  function changeModelCode() {
    queryParams.value.business = undefined;
    initBusinessList();
  }

  function changeModelName(model) {
    queryParams.value.model = model;
  }

  function initBusinessList() {
    getDicts(`gen_business_item_${queryParams.value.sysCode}_${queryParams.value.modelCode}`).then(
      (res) => {
        businessList.value = res.data;
      },
    );
  }

  // 点击联动下拉第三个下拉框触发方法
  function changeBusinessCode(businessCode) {
    queryParams.value.business = businessList.value.find(
      (item) => item.dictValue === businessCode,
    ).dictLabel;
  }

  function changeBusinessName(business) {
    queryParams.value.business = business;
  }

  getList();
</script>
